package com.zj.core.tools

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Handler
import android.os.IBinder
import android.preference.PreferenceManager
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import com.fileutils.BaseConstant
import com.fileutils.SPUtils
import com.moduleinterface.IStatus
import com.moduleinterface.InterfaceFactory
import com.moduleinterface.OnClickEventListener
import com.systemutils.SoftKeyBoardEvent
import com.systemutils.SoftKeyboardUtils
import com.systemutils.SystemUtil
import com.zj.core.R
import com.zj.core.base.BaseApplication
import com.zj.core.interfaces.TextMessageIml

class VoiceAsrTools{
    private var mRecording = false                     //录音状态

    /**
     * 按键按下操作
     */
    fun onKeyDown(keyCode: Int,activity:Activity?,binder: IBinder?){
        SoftKeyBoardEvent.keyClickEvent(object:OnClickEventListener{
            override fun leftClick() {}
            override fun centerClick() {}
            override fun maorenClick() {
                rightClick()
            }
            override fun rightClick() {
                onVoiceKeyDown(activity,binder)
            }
        }, keyCode)
    }

    /**
     * 语音按键
     */
    fun onVoiceKeyDown(activity:Activity?,binder: IBinder?){
        if (!SoftKeyboardUtils.hideSoftKeyBoard(activity,binder)){
            if (!mRecording) {
                startARouter(activity)
                if (voiceImageView?.visibility == View.GONE) {
                    voiceImageView?.visibility = View.VISIBLE
                }
                activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
                mRecording = true
            }
        }
    }

    /**
     * 按键抬起操作
     */
    fun onKeyUp(keyCode: Int,activity:Activity?){
        with(keyCode){
            if (SystemUtil.getSystemModel().contains(BaseConstant.PHONE_NAME_VERSION_2)
                || SystemUtil.getSystemModel().contains(BaseConstant.PHONE_NAME_VERSION_3)
            ) {                   //贸人翻译机
                if (this == 133) {
                    mRecording = false
                    closeVoice(activity)
                }
            } else if (SystemUtil.getSystemModel().contains(BaseConstant.PHONE_NAME_VERSION_1)) {             //双猴
                if (this == 132) {
                    mRecording = false
                    closeVoice(activity)
                }
            }else if(SystemUtil.getSystemModel().contains(BaseConstant.PHONE_NAME_VERSION_6)){                  //准儿
                if (this==131||keyCode == 132) {
                    mRecording = false
                    closeVoice(activity)
                }
            } else if (SystemUtil.getSystemModel().contains(BaseConstant.PHONE_NAME_VERSION_5)) {               //译呀译原生系统
                if (this == 267) {
                    mRecording = false
                    closeVoice(activity)
                }
            }
        }
    }

    /**
     * 关闭麦克风
     */
    private fun closeVoice(activity:Activity?){
        InterfaceFactory.getInstance().getmInterfaceASR().stopASR()
        if (voiceImageView?.visibility == View.VISIBLE) {
            voiceImageView?.visibility = View.GONE
        }
        activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    /**
     * ARouter服务
     */
    private fun startARouter(activity:Activity?){
        val sp = PreferenceManager.getDefaultSharedPreferences(activity)
        InterfaceFactory.getInstance().getmInterfaceASR().startASR(activity,voiceHandle,sp,
            SPUtils.getModelStatus(BaseApplication.context, Constant.MODEL_VOICE_KEY))
    }

    /**
     * 设置分贝动画
     */
    fun setVoiceImage(imgView:ImageView?):VoiceAsrTools{
        voiceImageView=imgView
        return this
    }

    /**
     * 识别内容回调
     */
    fun setHandleListener(handleIml: TextMessageIml):VoiceAsrTools{
        voiceHandleIml=handleIml
        return this
    }

    companion object {
        private var voiceHandleIml:TextMessageIml?=null
        @SuppressLint("StaticFieldLeak")
        private var voiceImageView:ImageView?=null

        /**
         * 语音识别Handler
         */
        private var voiceHandle=Handler{
            when (it.what){
                IStatus.STATUS_VOICE_CALLBACK -> {                                 //声音回调
                    voiceDbCallBack(Integer.parseInt(it.obj.toString()))
                }

                IStatus.STATUS_FINISHED -> {
                    if (!it.obj.toString().contains("识别错误") && it.arg2 == 1){    //正常结束
                        voiceHandleIml?.messageResult(it.obj.toString())
                    }
                }

                IStatus.STATUS_NONE, IStatus.STATUS_READY, IStatus.STATUS_SPEAKING, IStatus.STATUS_RECOGNITION -> {}
            }
            false
        }

        /**
         * 录音分贝回调
         *
         * @param record_db
         */
        private fun voiceDbCallBack(record_db: Int) {
            voiceImageView?.apply {
                when {
                    record_db < 2 -> setImageResource(R.mipmap.ease_record_animate_01)
                    record_db in 2..3 -> setImageResource(R.mipmap.ease_record_animate_02)
                    record_db in 4..5 -> setImageResource(R.mipmap.ease_record_animate_03)
                    record_db in 6..7 -> setImageResource(R.mipmap.ease_record_animate_04)
                    record_db in 8..9 -> setImageResource(R.mipmap.ease_record_animate_05)
                    record_db in 10..11 -> setImageResource(R.mipmap.ease_record_animate_06)
                    record_db in 12..13 -> setImageResource(R.mipmap.ease_record_animate_07)
                    record_db in 14..15 -> setImageResource(R.mipmap.ease_record_animate_08)
                    record_db in 16..17 -> setImageResource(R.mipmap.ease_record_animate_09)
                    record_db in 18..19 -> setImageResource(R.mipmap.ease_record_animate_10)
                    record_db in 20..21 -> setImageResource(R.mipmap.ease_record_animate_11)
                    record_db in 22..23 -> setImageResource(R.mipmap.ease_record_animate_12)
                    record_db in 24..25 -> setImageResource(R.mipmap.ease_record_animate_13)
                    record_db >= 26 -> setImageResource(R.mipmap.ease_record_animate_14)
                }
            }
        }
    }
}